package com.hksj.vsm.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.base.service.DefaultDeviceService;
import com.hksj.base.vo.DeviceOverviewVO;
import com.hksj.vsm.mapper.VideoDeviceInfoMapper;
import com.hksj.vsm.query.SpecialityVideoDeviceQuery;
import com.hksj.vsm.vo.PlayVideoVO;
import com.hksj.vsm.vo.SpecialityVideoDeviceVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class VideoDeviceInfoService {
    @Autowired
    private VideoDeviceInfoMapper videoDeviceInfoMapper;
    @Autowired
    private DefaultDeviceService defaultDeviceService;

    public Page<SpecialityVideoDeviceVO> pageDevice(SpecialityVideoDeviceQuery query) {
        Page<SpecialityVideoDeviceVO> page = new Page<>(query.getPageNum(), query.getPageSize());
        List<SpecialityVideoDeviceVO> list = videoDeviceInfoMapper.pageDevice(query,query.getDisAblePage() ? null : page);
        defaultDeviceService.wrapAddr(list);
        page.setRecords(list);
        return page;
    }

    public DeviceOverviewVO getDeviceOverview(SpecialityVideoDeviceQuery query) {
        return videoDeviceInfoMapper.getDeviceOverview(query);
    }

    /**
     * 根据设备id查询视频流
     * todo 对接第三方平台
     * @param deviceId
     * @return
     */
    public PlayVideoVO getPlayVideoInfo(String deviceId) {
        PlayVideoVO playVideoVO = videoDeviceInfoMapper.getPlayVideoInfo(deviceId);
        PlayVideoVO example = generateExample();
        BeanUtils.copyProperties(example,playVideoVO,"deviceId","name","paths");
        defaultDeviceService.wrapAddr(Arrays.asList(playVideoVO));
        return playVideoVO;
    }

    /**
     * 分页查询多个视频流
     * todo 对接第三方平台
     * @param query
     * @return
     */
    public Page<PlayVideoVO> pagePlayVideoInfo(SpecialityVideoDeviceQuery query) {
        Page<SpecialityVideoDeviceVO> devicePage = this.pageDevice(query);
        List<PlayVideoVO> res = new ArrayList<>();
        if (!ObjectUtils.isEmpty(devicePage.getRecords())){
            PlayVideoVO example = this.generateExample();
            for (SpecialityVideoDeviceVO specialityVideoDeviceVO : devicePage.getRecords()) {
                PlayVideoVO playVideoVO = new PlayVideoVO();
                BeanUtils.copyProperties(specialityVideoDeviceVO,playVideoVO);
                playVideoVO.setDeviceId(specialityVideoDeviceVO.getId());
                playVideoVO.setChannelId(example.getChannelId());
                playVideoVO.setStreamId(example.getStreamId());
                playVideoVO.setSnapshot(example.getSnapshot());
                playVideoVO.setPlayUrl(example.getPlayUrl());
                playVideoVO.setRtmp(example.getRtmp());
                playVideoVO.setRtsp(example.getRtsp());
                res.add(playVideoVO);
            }
        }
        Page<PlayVideoVO> page = new Page<>();
        page.setRecords(res);
        page.setTotal(devicePage.getTotal());
        return page;
    }

    private PlayVideoVO generateExample(){
        PlayVideoVO playVideoVO = new PlayVideoVO();
        playVideoVO.setChannelId("53030200001310000006");
        playVideoVO.setStreamId("gb_play_53030200001310000006_53030200001310000006");
        playVideoVO.setSnapshot("http://192.168.158.128:8888/vid/nice.png");
        playVideoVO.setPlayUrl("http://192.168.158.128:8888/vid/nice.mp4");
        playVideoVO.setRtmp("rtmp://118.195.177.177:1935/rtp/gb_play_53030200001310000006_53030200001310000006");
        playVideoVO.setRtsp("rtsp://118.195.177.177:554/rtp/gb_play_53030200001310000006_53030200001310000006");
        return playVideoVO;
    }
}
